package com.example.demo.controller;

import com.example.demo.controller.vo.ResponseResult;
import com.example.demo.entity.Image;
import com.example.demo.exception.BusinessException;
import com.example.demo.exception.ErrorCode;
import com.example.demo.service.ImageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/image")
public class ImageController {

    @Autowired
    private ImageService imageService;

    @PostMapping("/upload")
    public ResponseResult<List<Image>> uploadImages(
            @RequestHeader("token") String token,
            @RequestParam("image_class_id") Integer imageClassId,
            @RequestParam("img[]") MultipartFile[] files) {
        try {
            log.info("Uploading images: image_class_id={}, file_count={}", imageClassId, files.length);
            List<Image> uploadedImages = imageService.uploadImages(imageClassId, files);
            log.info("Images uploaded successfully: count={}", uploadedImages.size());
            return ResponseResult.success(uploadedImages);
        } catch (BusinessException e) {
            log.error("Image upload failed: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("Unexpected error during image upload", e);
            throw new BusinessException(ErrorCode.IMAGE_UPLOAD_FAILED, "Image upload failed");
        }
    }

    @PostMapping("/{id}")
    public ResponseResult<Boolean> updateImageName(
            @RequestHeader("token") String token,
            @PathVariable Integer id,
            @RequestParam String name) {
        try {
            log.info("Updating image name: id={}, new_name={}", id, name);
            boolean result = imageService.updateImageName(id, name);
            return ResponseResult.success(result);
        } catch (BusinessException e) {
            log.error("Failed to update image name: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("Unexpected error during image name update", e);
            throw new BusinessException(ErrorCode.IMAGE_UPLOAD_FAILED, "Failed to update image name");
        }
    }

    @PostMapping("/delete_all")
    public ResponseResult<Boolean> deleteImages(
            @RequestHeader("token") String token,
            @RequestBody DeleteImagesRequest request) {
        try {
            log.info("Deleting images: ids={}", request.getIds());
            boolean result = imageService.deleteImages(request.getIds());
            return ResponseResult.success(result);
        } catch (BusinessException e) {
            log.error("Failed to delete images: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("Unexpected error during image deletion", e);
            throw new BusinessException(ErrorCode.IMAGE_UPLOAD_FAILED, "Failed to delete images");
        }
    }

    public static class DeleteImagesRequest {
        private List<Integer> ids;

        public List<Integer> getIds() {
            return ids;
        }

        public void setIds(List<Integer> ids) {
            this.ids = ids;
        }
    }
}
